package rexsee.core.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class Slider extends LinearLayout {
	private final TextView titleText;
	private final TextView beginText;
	private final TextView endText;
	private final TextView seekText;
	private final SeekBar seekBar;
	private final float mDensity;
	private int scale(int pixel) {
		return Math.round(mDensity * pixel);
	}
	public Slider(Context context, final String title, final int begin, final int end, final int init, final OnSliderChangeListener listener) {
		super(context);
		mDensity = context.getResources().getDisplayMetrics().density;
		this.setOrientation(LinearLayout.HORIZONTAL);
		this.setGravity(Gravity.CENTER_VERTICAL);
		titleText = new TextView(context);
		if (title != null && !title.equals("")) {
			titleText.setTextSize(18);
			titleText.setPadding(0, 0, scale(5), 0);
			titleText.setTextColor(Color.WHITE);
			titleText.setText(Html.fromHtml(title));
			this.addView(titleText, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		}
		LinearLayout sLayout = new LinearLayout(context);
		sLayout.setOrientation(VERTICAL);
		LinearLayout textBar = new LinearLayout(context);
		textBar.setOrientation(LinearLayout.HORIZONTAL);
		beginText = new TextView(context);
		beginText.setGravity(Gravity.LEFT);
		beginText.setTextSize(16);
		beginText.setTextColor(Color.WHITE);
		beginText.setText(String.valueOf(begin));
		textBar.addView(beginText, new LinearLayout.LayoutParams(scale(60), scale(24)));
		seekText = new TextView(context);
		seekText.setGravity(Gravity.CENTER_HORIZONTAL);
		seekText.setTextSize(16);
		seekText.setTextColor(Color.parseColor("#F8E81D"));
		seekText.setText(String.valueOf(init));
		textBar.addView(seekText, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, scale(24), 2));
		endText = new TextView(context);
		endText.setGravity(Gravity.RIGHT);
		endText.setTextSize(16);
		endText.setTextColor(Color.WHITE);
		endText.setText(String.valueOf(end));
		textBar.addView(endText, new LinearLayout.LayoutParams(scale(60), scale(24)));
		sLayout.addView(textBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		seekBar = new SeekBar(context);
		seekBar.setProgress((init - begin) * 100 / (end - begin));
		seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				seekText.setText(String.valueOf(begin + progress * (end - begin) / 100));
				if (listener != null) listener.run(progress);
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		sLayout.addView(seekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		this.addView(sLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
	}
	public static abstract class OnSliderChangeListener {
		public abstract void run(int progress);
	}
	public String getValue() {
		return seekText.getText().toString();
	}
	public void setValue(int value) {
		seekText.setText(String.valueOf(value));
		seekBar.setProgress(value * 100 / 255);
	}
	public void setValue(String value) {
		seekText.setText(value);
		seekBar.setProgress(Integer.parseInt(value) * 100 / 255);
	}
}
